package com.um.springcloud.dubbo.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

/**
 * 设置project.name系统属性，服务注册中心会用到
 *
 * @author 甘天伟
 * @since 2022/9/20
 * 版权所有 广州优匠科技有限公司
 */
@Slf4j
public class SetProjectNameListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        Environment env = event.getEnvironment();
        String projectName = env.getProperty("spring.application.instance-name");
        if (projectName == null) {
            projectName = env.getProperty("spring.application.name");
        }
        if (projectName != null) {
            log.info("Set project.name: {}", projectName);
            System.setProperty("project.name", projectName);
        }
    }
}
